﻿using System.Collections.Generic;
using System.Linq;

namespace TakePhotoTogether.Communication
{
    public class DataQueue
    {
        private Queue<DataPacket> dataBuffer = null;

        public DataQueue()
        {
            CreateQueue();
        }

        protected virtual void CreateQueue()
        {
            if (dataBuffer == null)
                dataBuffer = new Queue<DataPacket>();
        }

        public virtual void Clear()
        {
            dataBuffer.Clear();
        }

        public virtual void Enqueue(DataPacket packet)
        {
            dataBuffer.Enqueue(packet);
        }

        public virtual DataPacket Dequeue()
        {
            return dataBuffer.Dequeue();
        }

        public virtual int Count()
        {
            return dataBuffer.Count;
        }

        /// <summary>
        /// Get the number of packets on queue that share the same message type
        /// </summary>
        /// <param name="messageType"></param>
        /// <returns></returns>
        public virtual int GetNumberOfPacketsOnQueue(int messageType)
        {
            int packetCount = 0;
            int bufferCount = dataBuffer.Count;

            for (int q = 0; q < bufferCount; q++)
            {
                DataPacket packet  = dataBuffer.ElementAt<DataPacket>(q);

                if (packet.MessageType == messageType)
                {
                    packetCount++;
                }
            }

            return packetCount;
        }


        /// <summary>
        /// Get the number of packets on queue
        /// </summary>
        /// <param name="messageType"></param>
        /// <returns></returns>
        public virtual int GetNumberOfPacketsOnQueue()
        {
            return dataBuffer.Count;
        }
    }
}
